package auntie.production.app2.db;

import java.io.Serializable;
import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTable;
import com.j256.ormlite.table.TableUtils;

public class auntie_production_app_db extends OrmLiteSqliteOpenHelper {

    public static final String LOG = auntie_production_app_db.class.getName();
    public static final String DATABASE_NAME = "auntie_production_app_db";
    public static final int VERSION = 1;

    // tables
    public Dao<productions, Integer> dao_productions;
    public Dao<production_details, Integer> dao_production_details;

    public auntie_production_app_db(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    private static final String[] DB_CREATE_TABLES = {
            "create table if not exists productions ( \n"
                    + "  id integer primary key autoincrement \n"
                    + " ,inputer varchar not null \n"
                    + " ,total_amount double default 0 \n"
                    + " ,submitted tinyint default false \n"
                    + " ,created timestamp default current_timestamp \n"
                    + " ,modified datetime not null \n" + "); \n",

            "create table if not exists production_details ( \n"
                    + "  id integer primary key autoincrement \n"
                    + " ,production_id integer not null \n"
                    + " ,product_name varchar not null \n"
                    + " ,sticker varchar not null \n"
                    + " ,price double default 0 \n"
                    + " ,quantity double default 0 \n"
                    + " ,amount double default 0 \n"
                    + " ,input_time datetime not null \n"
                    + " ,cancelled tinyint default false \n"
                    + " ,created timestamp default current_timestamp \n"
                    + "); \n" };

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource cs) {

        
        try {
            Log.i(LOG, "onCreate");

            String[] tablesCreate = DB_CREATE_TABLES;
            if (tablesCreate == null)
                return;
            for (int i = 0; i < tablesCreate.length; i++) {
                try {
                    db.execSQL(tablesCreate[i]);
                } catch (Exception e) {
                    Log.e(LOG, e.getMessage());
                    continue;
                }
            }

            TableUtils.createTable(cs, productions.class);
            TableUtils.createTable(cs, production_details.class);

        } catch (Exception e) {
            Log.e(LOG, "can't create database");

        }
    }

    @Override
    public void onOpen(SQLiteDatabase db) {

        try {
            this.onCreate(db);
        } catch (Exception e) {
            Log.e(LOG, "already created");
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource cs, int ov, int nv) {
        try {
            Log.i(LOG, "onUpgrade");
            TableUtils.dropTable(cs, productions.class, true);
            TableUtils.dropTable(cs, production_details.class, true);
            onCreate(db, cs);
        } catch (Exception e) {
            Log.i(LOG, "can't drop database", e);
            throw new RuntimeException(e);
        }

    }

    public Cursor rawQuery(String q) {
        return getReadableDatabase().rawQuery(q, null);
    }

    public Dao<productions, Integer> dao_productions() {
        try {
            if (dao_productions == null)
                dao_productions = getDao(productions.class);
            return dao_productions;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Dao<production_details, Integer> dao_production_details() {
        try {
            if (dao_production_details == null)
                dao_production_details = getDao(production_details.class);
            return dao_production_details;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // data here
    @DatabaseTable
    public static class productions implements Serializable {

        private static final long serialVersionUID = 1L;

        @DatabaseField(generatedId = true)
        private int id;
        @DatabaseField
        private String inputer;
        @DatabaseField
        private double total_amount;
        @DatabaseField
        private boolean submitted;
        @DatabaseField
        private Date created = new Date();
        @DatabaseField
        private Date modified;

        public productions(String inputer, Date modified) {
            super();
            this.inputer = inputer;
            this.modified = modified;
        }

        public productions() {
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getInputer() {
            return inputer;
        }

        public void setInputer(String inputer) {
            this.inputer = inputer;
        }

        public double getTotal_amount() {
            return total_amount;
        }

        public void setTotal_amount(double totalAmount) {
            total_amount = totalAmount;
        }

        public boolean isSubmitted() {
            return submitted;
        }

        public void setSubmitted(boolean submitted) {
            this.submitted = submitted;
        }

        public Date getCreated() {
            return created;
        }

        public void setCreated(Date created) {
            this.created = created;
        }

        public Date getModified() {
            return modified;
        }

        public void setModified(Date modified) {
            this.modified = modified;
        }

        public static long getSerialversionuid() {
            return serialVersionUID;
        }

        @Override
        public String toString() {
            return "productions [getCreated()=" + getCreated() + ", getId()="
                    + getId() + ", getInputer()=" + getInputer()
                    + ", getModified()=" + getModified()
                    + ", getTotal_amount()=" + getTotal_amount()
                    + ", isSubmitted()=" + isSubmitted() + "]";
        }

    } // productions

    @DatabaseTable
    public static class production_details implements Serializable {

        private static final long serialVersionUID = 1L;
        @DatabaseField(generatedId = true)
        private int id;
        @DatabaseField
        private int production_id;
        @DatabaseField
        private String product_name;
        @DatabaseField
        private String sticker;
        @DatabaseField
        private double price;
        @DatabaseField
        private double quantity;
        @DatabaseField
        private double amount;
        @DatabaseField
        private Date input_time;
        @DatabaseField
        private boolean cancelled;
        @DatabaseField
        private Date created = new Date();

        public production_details(int productionId, String productName,
                String sticker, double price, double quantity, double amount,
                Date inputTime) {
            production_id = productionId;
            product_name = productName;
            this.sticker = sticker;
            this.price = price;
            this.quantity = quantity;
            this.amount = amount;
            input_time = inputTime;
        }

        public production_details() {
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getProduction_id() {
            return production_id;
        }

        public void setProduction_id(int productionId) {
            production_id = productionId;
        }

        public String getProduct_name() {
            return product_name;
        }

        public void setProduct_name(String productName) {
            product_name = productName;
        }

        public String getSticker() {
            return sticker;
        }

        public void setSticker(String sticker) {
            this.sticker = sticker;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public double getQuantity() {
            return quantity;
        }

        public void setQuantity(double quantity) {
            this.quantity = quantity;
        }

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount = amount;
        }

        public Date getInput_time() {
            return input_time;
        }

        public void setInput_time(Date inputTime) {
            input_time = inputTime;
        }

        public boolean isCancelled() {
            return cancelled;
        }

        public void setCancelled(boolean cancelled) {
            this.cancelled = cancelled;
        }

        public Date getCreated() {
            return created;
        }

        public void setCreated(Date created) {
            this.created = created;
        }

        @Override
        public String toString() {
            return "production_details [amount=" + amount + ", cancelled="
                    + cancelled + ", created=" + created + ", id=" + id
                    + ", input_time=" + input_time + ", price=" + price
                    + ", product_name=" + product_name + ", production_id="
                    + production_id + ", quantity=" + quantity + ", sticker="
                    + sticker + "]";
        }

    }

}
